package cn.gov.msa.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;

import cn.gov.msa.model.Apply;
import cn.gov.msa.service.ApplyService;
import cn.gov.msa.util.GsonHelper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@RestController
@RequestMapping(value = "/apply", produces = "text/plain;charset=UTF-8")
public class ApplyController {
	
	ObjectMapper mapper = new ObjectMapper();
	
	@Autowired
	private ApplyService applyService;
		
	@RequestMapping(value = "/list")
	public String list(@RequestBody JSONObject params) throws Exception {	
		
		String result = "";		
		int pageNum = 1;
		int pageSize = 5;
		System.out.println(params);
		if(params.has("pageNum"))
		{
			pageNum = params.getInt("pageNum");
			params.remove("pageNum");
		}
		if(params.has("pageSize"))
		{
			pageSize = params.getInt("pageSize");
			params.remove("pageSize");
		}	
		Apply apply = GsonHelper.convertEntity(params.toString(), Apply.class);
		
		List<Apply> applyList =applyService.selectData(pageNum, pageSize, apply);	
	    //获取分页信息
        PageInfo page = new PageInfo(applyList);       
		JSONArray ja = JSONArray.fromObject(applyList);				
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", ja);				
		result = jsonResult.toString();
		return result;
	}

}
